import React from 'react';
import { Modal, Form, Input, Select } from 'antd';
import {
  ONLINESTATUS,
  EQUIPMENT
} from 'configs/constants';
import { isEmpty } from 'utils/share';

const FormItem = Form.Item;
const Option = Select.Option;

class AddModal extends React.Component {
  constructor (props) {
    super(props);
    this.state = {};
    this.onOK = this.onOK.bind(this);
  }

  onOK () {
    const { onOK, form, onCancel, record, getList } = this.props;
    form.validateFields((err, values) => {
      if (!err) {
        onOK({ ...values, id: record.id }).then(() => {
          onCancel();
          getList();
        });
      }
    });
  }

  render () {
    const { onCancel, form, loading, record = {} } = this.props;
    const { getFieldDecorator } = form;
    const modalOpts = {
      confirmLoading: loading,
      title: `${record.id ? '编辑' : '创建'}设备`,
      width: 500,
      visible: true,
      maskClosable: false,
      okText: '确定',
      cancelText: '取消',
      onOk: this.onOK,
      onCancel
    };
    const formItemLayout = {
      labelCol: {
        span: 6
      },
      wrapperCol: {
        span: 18
      }
    };
    return (
      <Modal {...modalOpts}>
        <Form>
          <FormItem
            label='设备编号'
            {...formItemLayout}
          >
            {
              getFieldDecorator('equipmentNo', {
                initialValue: record.equipmentNo || '',
                rules: [{ required: true, message: '请填写设备编号' }]
              })(
                <Input
                  maxLength={30}
                  placeholder='设备编号'
                />
              )
            }
          </FormItem>
          <FormItem
            label='是否开启'
            {...formItemLayout}
          >
            {
              getFieldDecorator('onlineStatus', {
                initialValue: !isEmpty(record.onlineStatus) ? `${record.onlineStatus}` : undefined,
                rules: [
                  { required: true, message: '是否开启不能为空' }
                ]
              })(
                <Select
                  placeholder='请选择是否开启'
                >
                  {
                  Object.keys(ONLINESTATUS).map((item, key) => 
                    <Option value={item} key={item}>{ONLINESTATUS[item]}</Option>)
                  }
                </Select>
              )
            }
          </FormItem>
          <FormItem
            label='金属板编号'
            {...formItemLayout}
          >
            {
              getFieldDecorator('plateNo', {
                initialValue: record.plateNo || '',
                rules: [{ required: true, message: '请填写金属板编号' }]
              })(
                <Input
                  maxLength={30}
                  placeholder='金属板编号'
                />
              )
            }
          </FormItem>
          <FormItem
            label='类型'
            {...formItemLayout}
          >
            {
              getFieldDecorator('type', {
                initialValue: !isEmpty(record.type) ? `${record.type}` : undefined,
                rules: [
                  { required: true, message: '类型不能为空' }
                ]
              })(
                <Select
                  placeholder='请选择类型'
                >
                  {
                  Object.keys(EQUIPMENT).map((item, key) => 
                    <Option value={item} key={item}>{EQUIPMENT[item]}</Option>)
                  }
                </Select>
              )
            }
          </FormItem>
        </Form>
      </Modal>
    );
  }
}

export default Form.create()(AddModal);
